let user = {
    id: 1001,
    name: "张三",
    phone: 12345678987
};
let users = [
    {
        id: 1001, name: "小花", phone: "17898765432", flag: true
    },
    {
        id: 1002, name: "大庆", phone: "17898765432", flag: true
    },
    {
        id: 1003, name: "白云", phone: "17898765432", flag: true
    },
    {
        id: 1004, name: "阿花", phone: "17898765432", flag: false
    }
];
// 1.加载核心模块
const http = require("http");//导入核心模块
const fs = require("fs");//导入核心模块
const template = require("art-template");//导入三方模块
console.log(template);


// 2.创建服务器功能函数
const server = http.createServer(function (request, response) {
    let path = request.url;//请求路径
    console.log(path);
    switch (path) {
        case "/user":
            let buffer1 = fs.readFileSync("./template/user.html");
            let str1 = buffer1.toString();

            // str1.replace(需要被替换的内容,替换的新内容)
            str1 = str1.replace("{{id}}", user.id);
            str1 = str1.replace("{{name}}", user.name);
            str1 = str1.replace("{{phone}}", user.phone);
            response.end(str1);
            break;
        case "/user2":
            let buffer11 = fs.readFileSync("./template/user.html");
            let str11 = buffer11.toString();//string
           
            let userStr = template.render(str11, user);
            response.end(userStr);
            break;
        case "/userList":
            let buffer2 = fs.readFileSync("./template/list.html");
            let str2 = buffer2.toString();//string
            let strs = "";
            for (let i = 0; i < users.length; i++) {
                strs += `
                    <tr>
                        <td>${users[i].id}</td>
                        <td>${users[i].name}</td>
                        <td>${users[i].phone}</td>
                        <td>${users[i].flag ? '是' : '否'}</td>
                    </tr>
                `;
            }
            str2 = str2.replace("{{list}}", strs);
            response.end(str2);
            break;

        case "/userList2":
            let buffer4 = fs.readFileSync("./template/list2.html");
            let str4 = buffer4.toString();//string
            console.log(str4);
            let strs2=template.render(str4,{userList:users});
            response.end(strs2);
            break;

    }


});


// 3.启动服务器
server.listen(3000);
